home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / compress / tar321__.zip / SOURCES.ZIP / MATCH.S < prev    next >
Text File  |  1996-07-03  |  14KB  |  384 lines

  1. /*
  2.  * Copyright (C) 1990-1993 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  3.  * Kai Uwe Rommel and Igor Mandrichenko.
  4.  * Permission is granted to any individual or institution to use, copy, or
  5.  * redistribute this software so long as all of the original files are
  6.  * included, that it is not sold for profit, and that this copyright notice is
  7.  * retained.
  8.  *
  9.  * match.s by Jean-loup Gailly. Translated to 32 bit code by Kai Uwe Rommel.
  10.  * The 68020 version has been written by Francesco Potorti` <pot@cnuce.cnr.it>
  11.  * with adaptations by Carsten Steger <stegerc@informatik.tu-muenchen.de>,
  12.  * Andreas Schwab <schwab@lamothe.informatik.uni-dortmund.de> and
  13.  * Kristoffer Eriksson <ske@pkmab.se>
  14.  */
  15.  
  16. /* Preprocess with -DNO_UNDERLINE if your C compiler does not prefix
  17.  * external symbols with an underline character '_'.
  18.  */
  19. #if defined(NO_UNDERLINE) || defined(SYSV)
  20. #  define _prev             prev
  21. #  define _window           scope
  22. #  define _match_start      match_start
  23. #  define _prev_length      prev_length
  24. #  define _good_match       good_match
  25. #  define _nice_match       nice_match
  26. #  define _strstart         strstart
  27. #  define _max_chain_length max_chain_length
  28.  
  29. #  define _match_init       match_init
  30. #  define _longest_match    longest_match
  31. #else
  32. #  define _window           _scope
  33. #endif
  34.  
  35. #ifdef DYN_ALLOC
  36.   error: DYN_ALLOC not yet supported in match.s
  37. #endif
  38.  
  39. #if defined(i386) || defined(_I386)
  40.  
  41. /* This version is for 386 Unix or OS/2 in 32 bit mode.
  42.  * Warning: it uses the AT&T syntax: mov source,dest
  43.  * This file is only optional. If you want to force the C version,
  44.  * add -DNO_ASM to CFLAGS in Makefile and set OBJA to an empty string.
  45.  * If you have reduced WSIZE in gzip.h, then change its value below.
  46.  * This version assumes static allocation of the arrays (-DDYN_ALLOC not used).
  47.  */
  48.  
  49.         .file   "match.S"
  50.  
  51. #define MAX_MATCH       258
  52. #define MAX_MATCH2      $128 /* MAX_MATCH/2-1 */
  53. #define MIN_MATCH       3
  54. #define    WSIZE        $32768
  55. #define MAX_DIST        WSIZE - MAX_MATCH - MIN_MATCH - 1
  56.  
  57.         .globl  _match_init
  58.         .globl  _longest_match
  59.  
  60.         .text
  61.  
  62. _match_init:
  63.         ret
  64.  
  65. /*-----------------------------------------------------------------------
  66.  * Set match_start to the longest match starting at the given string and
  67.  * return its length. Matches shorter or equal to prev_length are discarded,
  68.  * in which case the result is equal to prev_length and match_start is
  69.  * garbage.
  70.  * IN assertions: cur_match is the head of the hash chain for the current
  71.  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  72.  */
  73.  
  74. _longest_match: /* int longest_match(cur_match) */
  75.  
  76. #define cur_match   20(%esp)
  77.      /* return address */               /* esp+16 */
  78.         push    %ebp                    /* esp+12 */
  79.         push    %edi                    /* esp+8  */
  80.         push    %esi                    /* esp+4  */
  81.         push    %ebx                    /* esp    */
  82.  
  83. /*
  84.  *      match        equ esi
  85.  *      scan         equ edi
  86.  *      chain_length equ ebp
  87.  *      best_len     equ ebx
  88.  *      limit        equ edx
  89.  */
  90.         mov     cur_match,%esi
  91.         mov     _max_chain_length,%ebp /* chain_length = max_chain_length */
  92.         mov     _strstart,%edi
  93.         mov     %edi,%edx
  94.         sub     MAX_DIST,%edx          /* limit = strstart-MAX_DIST */
  95.         jae     limit_ok
  96.         sub     %edx,%edx              /* limit = NIL */
  97. limit_ok:
  98.         add     $2+_window,%edi        /* edi = offset(window+strstart+2) */
  99.         mov     _prev_length,%ebx      /* best_len = prev_length */
  100.         movw    -3(%ebx,%edi),%ax      /* ax = scan[best_len-1..best_len] */
  101.         movw    -2(%edi),%cx           /* cx = scan[0..1] */
  102.         cmp     _good_match,%ebx       /* do we have a good match already? */
  103.         jb      do_scan
  104.         shr     $2,%ebp                /* chain_length >>= 2 */
  105.         jmp     do_scan
  106.  
  107.         .align  4
  108. long_loop:
  109. /* at this point, edi == scan+2, esi == cur_match */
  110.         movw    -3(%ebx,%edi),%ax       /* ax = scan[best_len-1..best_len] */
  111.         movw     -2(%edi),%cx           /* cx = scan[0..1] */
  112. short_loop:
  113. /*
  114.  * at this point, di == scan+2, si == cur_match,
  115.  * ax = scan[best_len-1..best_len] and cx = scan[0..1]
  116.  */
  117.         and     WSIZE-1, %esi
  118.         movw    _prev(%esi,%esi),%si    /* cur_match = prev[cur_match] */
  119.                                         /* top word of esi is still 0 */
  120.         cmp     %edx,%esi               /* cur_match <= limit ? */
  121.         jbe     the_end
  122.         dec     %ebp                    /* --chain_length */
  123.         jz      the_end
  124. do_scan:
  125.         cmpw    _window-1(%ebx,%esi),%ax/* check match at best_len-1 */
  126.         jne     short_loop
  127.         cmpw    _window(%esi),%cx       /* check min_match_length match */
  128.         jne     short_loop
  129.  
  130.         lea     _window+2(%esi),%esi    /* si = match */
  131.         mov     %edi,%eax               /* ax = scan+2 */
  132.         mov     MAX_MATCH2,%ecx         /* scan for at most MAX_MATCH bytes */
  133.         rep;    cmpsw                   /* loop until mismatch */
  134.         je      maxmatch                /* match of length MAX_MATCH? */
  135. mismatch:
  136.         movb    -2(%edi),%cl        /* mismatch on first or second byte? */
  137.         subb    -2(%esi),%cl        /* cl = 0 if first bytes equal */
  138.         xchg    %edi,%eax           /* edi = scan+2, eax = end of scan */
  139.         sub     %edi,%eax           /* eax = len */
  140.         sub     %eax,%esi           /* esi = cur_match + 2 + offset(window) */
  141.         sub     $2+_window,%esi     /* esi = cur_match */
  142.         subb    $1,%cl              /* set carry if cl == 0 (cannot use DEC) */
  143.         adc     $0,%eax             /* eax = carry ? len+1 : len */
  144.         cmp     %ebx,%eax           /* len > best_len ? */
  145.         jle     long_loop
  146.         mov     %esi,_match_start       /* match_start = cur_match */
  147.         mov     %eax,%ebx               /* ebx = best_len = len */
  148.         cmp     _nice_match,%eax        /* len >= nice_match ? */
  149.         jl      long_loop
  150. the_end:
  151.         mov     %ebx,%eax               /* result = eax = best_len */
  152.         pop     %ebx
  153.         pop     %esi
  154.         pop     %edi
  155.         pop     %ebp
  156.         ret
  157. maxmatch:
  158.         cmpsb
  159.         jmp     mismatch
  160.  
  161. #else
  162.  
  163. /* ======================== 680x0 version ================================= */
  164.  
  165. #if defined(m68k)||defined(mc68k)||defined(__mc68000__)||defined(__MC68000__)
  166. #  ifndef mc68000
  167. #    define mc68000
  168. #  endif
  169. #endif
  170.  
  171. #if defined(__mc68020__) || defined(__MC68020__) || defined(sysV68)
  172. #  ifndef mc68020
  173. #    define mc68020
  174. #  endif
  175. #endif
  176.  
  177. #if defined(mc68020) || defined(mc68000)
  178.  
  179. #if (defined(mc68020) || defined(NeXT)) && !defined(UNALIGNED_OK)
  180. #  define UNALIGNED_OK
  181. #endif
  182.  
  183. #ifdef sysV68  /* Try Motorola Delta style */
  184.  
  185. #  define GLOBAL(symbol)        global  symbol
  186. #  define TEXT                  text
  187. #  define FILE(filename)        file    filename
  188. #  define invert_maybe(src,dst) dst,src
  189. #  define imm(data)             &data
  190. #  define reg(register)         %register
  191.  
  192. #  define addl                  add.l
  193. #  define addql                 addq.l
  194. #  define blos                  blo.b
  195. #  define bhis                  bhi.b
  196. #  define bras                  bra.b
  197. #  define clrl                  clr.l
  198. #  define cmpmb                 cmpm.b
  199. #  define cmpw                  cmp.w
  200. #  define cmpl                  cmp.l
  201. #  define lslw                  lsl.w
  202. #  define lsrl                  lsr.l
  203. #  define movel                 move.l
  204. #  define movew                 move.w
  205. #  define moveb                 move.b
  206. #  define moveml                movem.l
  207. #  define subl                  sub.l
  208. #  define subw                  sub.w
  209. #  define subql                 subq.l
  210.  
  211. #  define IndBase(bd,An)        (bd,An)
  212. #  define IndBaseNdxl(bd,An,Xn) (bd,An,Xn.l)
  213. #  define IndBaseNdxw(bd,An,Xn) (bd,An,Xn.w)
  214. #  define predec(An)            -(An)
  215. #  define postinc(An)           (An)+
  216.  
  217. #else /* default style (Sun 3, NeXT, Amiga, Atari) */
  218.  
  219. #  define GLOBAL(symbol)        .globl  symbol
  220. #  define TEXT                  .text
  221. #  define FILE(filename)        .even
  222. #  define invert_maybe(src,dst) src,dst
  223. #  if defined(sun) || defined(mc68k)
  224. #    define imm(data)           #data
  225. #  else
  226. #    define imm(data)           \#data
  227. #  endif
  228. #  define reg(register)         register
  229.  
  230. #  define blos                  bcss
  231. #  if defined(sun) || defined(mc68k)
  232. #    define movel               movl
  233. #    define movew               movw
  234. #    define moveb               movb
  235. #  endif
  236. #  define IndBase(bd,An)        An@(bd)
  237. #  define IndBaseNdxl(bd,An,Xn) An@(bd,Xn:l)
  238. #  define IndBaseNdxw(bd,An,Xn) An@(bd,Xn:w)
  239. #  define predec(An)            An@-
  240. #  define postinc(An)           An@+
  241.  
  242. #endif  /* styles */
  243.  
  244. #define Best_Len        reg(d0)         /* unsigned */
  245. #define Cur_Match       reg(d1)         /* Ipos */
  246. #define Loop_Counter    reg(d2)         /* int */
  247. #define Scan_Start      reg(d3)         /* unsigned short */
  248. #define Scan_End        reg(d4)         /* unsigned short */
  249. #define Limit           reg(d5)         /* IPos */
  250. #define Chain_Length    reg(d6)         /* unsigned */
  251. #define Scan_Test       reg(d7)
  252. #define Scan            reg(a0)         /* *uch */
  253. #define Match           reg(a1)         /* *uch */
  254. #define Prev_Address    reg(a2)         /* *Pos */
  255. #define Scan_Ini        reg(a3)         /* *uch */
  256. #define Match_Ini       reg(a4)         /* *uch */
  257. #define Stack_Pointer   reg(sp)
  258.  
  259. #define MAX_MATCH       258
  260. #define MIN_MATCH       3
  261. #define WSIZE           32768
  262. #define MAX_DIST        (WSIZE - MAX_MATCH - MIN_MATCH - 1)
  263.  
  264.         GLOBAL  (_match_init)
  265.         GLOBAL  (_longest_match)
  266.  
  267.         TEXT
  268.  
  269.         FILE    ("match.S")
  270.  
  271. _match_init:
  272.         rts
  273.  
  274. /*-----------------------------------------------------------------------
  275.  * Set match_start to the longest match starting at the given string and
  276.  * return its length. Matches shorter or equal to prev_length are discarded,
  277.  * in which case the result is equal to prev_length and match_start is
  278.  * garbage.
  279.  * IN assertions: cur_match is the head of the hash chain for the current
  280.  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  281.  */
  282.  
  283. /* int longest_match (cur_match) */
  284.  
  285. #ifdef UNALIGNED_OK
  286. #  define pushreg       15928           /* d2-d6/a2-a4 */
  287. #  define popreg        7292
  288. #else
  289. #  define pushreg       16184           /* d2-d7/a2-a4 */
  290. #  define popreg        7420
  291. #endif
  292.  
  293. _longest_match:
  294.         movel   IndBase(4,Stack_Pointer),Cur_Match
  295.         moveml  imm(pushreg),predec(Stack_Pointer)
  296.         movel   _max_chain_length,Chain_Length
  297.         movel   _prev_length,Best_Len
  298.         movel   imm(_prev),Prev_Address
  299.         movel   imm(_window+MIN_MATCH),Match_Ini
  300.         movel   _strstart,Limit
  301.         movel   Match_Ini,Scan_Ini
  302.         addl    Limit,Scan_Ini
  303.         subw    imm(MAX_DIST),Limit
  304.         bhis    L__limit_ok
  305.         clrl    Limit
  306. L__limit_ok:
  307.         cmpl    invert_maybe(_good_match,Best_Len)
  308.         blos    L__length_ok
  309.         lsrl    imm(2),Chain_Length
  310. L__length_ok:
  311.         subql   imm(1),Chain_Length
  312. #ifdef UNALIGNED_OK
  313.         movew   IndBase(-MIN_MATCH,Scan_Ini),Scan_Start
  314.         movew   IndBaseNdxw(-MIN_MATCH-1,Scan_Ini,Best_Len),Scan_End
  315. #else
  316.         moveb   IndBase(-MIN_MATCH,Scan_Ini),Scan_Start
  317.         lslw    imm(8),Scan_Start
  318.         moveb   IndBase(-MIN_MATCH+1,Scan_Ini),Scan_Start
  319.         moveb   IndBaseNdxw(-MIN_MATCH-1,Scan_Ini,Best_Len),Scan_End
  320.         lslw    imm(8),Scan_End
  321.         moveb   IndBaseNdxw(-MIN_MATCH,Scan_Ini,Best_Len),Scan_End
  322. #endif
  323.         bras    L__do_scan
  324.  
  325. L__long_loop:
  326. #ifdef UNALIGNED_OK
  327.         movew   IndBaseNdxw(-MIN_MATCH-1,Scan_Ini,Best_Len),Scan_End
  328. #else
  329.         moveb   IndBaseNdxw(-MIN_MATCH-1,Scan_Ini,Best_Len),Scan_End
  330.         lslw    imm(8),Scan_End
  331.         moveb   IndBaseNdxw(-MIN_MATCH,Scan_Ini,Best_Len),Scan_End
  332. #endif
  333.  
  334. L__short_loop:
  335.         lslw    imm(1),Cur_Match
  336.         movew   IndBaseNdxl(0,Prev_Address,Cur_Match),Cur_Match
  337.         cmpw    invert_maybe(Limit,Cur_Match)
  338.         dbls    Chain_Length,L__do_scan
  339.         bras    L__return
  340.  
  341. L__do_scan:
  342.         movel   Match_Ini,Match
  343.         addl    Cur_Match,Match
  344. #ifdef UNALIGNED_OK
  345.         cmpw    invert_maybe(IndBaseNdxw(-MIN_MATCH-1,Match,Best_Len),Scan_End)
  346.         bne     L__short_loop
  347.         cmpw    invert_maybe(IndBase(-MIN_MATCH,Match),Scan_Start)
  348.         bne     L__short_loop
  349. #else
  350.         moveb   IndBaseNdxw(-MIN_MATCH-1,Match,Best_Len),Scan_Test
  351.         lslw    imm(8),Scan_Test
  352.         moveb   IndBaseNdxw(-MIN_MATCH,Match,Best_Len),Scan_Test
  353.         cmpw    invert_maybe(Scan_Test,Scan_End)
  354.         bne     L__short_loop
  355.         moveb   IndBase(-MIN_MATCH,Match),Scan_Test
  356.         lslw    imm(8),Scan_Test
  357.         moveb   IndBase(-MIN_MATCH+1,Match),Scan_Test
  358.         cmpw    invert_maybe(Scan_Test,Scan_Start)
  359.         bne     L__short_loop
  360. #endif
  361.  
  362.         movew   imm((MAX_MATCH-MIN_MATCH+1)-1),Loop_Counter
  363.         movel   Scan_Ini,Scan
  364. L__scan_loop:
  365.         cmpmb   postinc(Match),postinc(Scan)
  366.         dbne    Loop_Counter,L__scan_loop
  367.  
  368.         subl    Scan_Ini,Scan
  369.         addql   imm(MIN_MATCH-1),Scan
  370.         cmpl    invert_maybe(Best_Len,Scan)
  371.         bls     L__short_loop
  372.         movel   Scan,Best_Len
  373.         movel   Cur_Match,_match_start
  374.         cmpl    invert_maybe(_nice_match,Best_Len)
  375.         blos    L__long_loop
  376. L__return:
  377.         moveml  postinc(Stack_Pointer),imm(popreg)
  378.         rts
  379.  
  380. #else
  381.  error: this asm version is for 386 or 680x0 only
  382. #endif /* mc68000 || mc68020 */
  383. #endif /* i386 || _I386   */
  384.